package com.bootdo.system.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import net.sf.json.JSONArray;

public class MapUtil {

	//wanghu 个人
	public static final String BAIDU_AK="5bebsTWv4ZNQFfx6VeWYxsTteKqW9sKt";
	
	 public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
	        if (map == null)  
	            return null;  
	        Object obj = beanClass.newInstance();  
	        org.apache.commons.beanutils.BeanUtils.populate(obj, map);  
	        return obj;  
	    }    
	      
	    public static Map<?, ?> objectToMap(Object obj) {  
	        if(obj == null)  
	            return null;   
	        return new org.apache.commons.beanutils.BeanMap(obj);  
	    }    
	    
	    public static void main(String[] args) throws IOException {  
	       System.out.println("根据名称找经纬度=" + getLocationByName("长阳地铁站")); 
	       System.out.println("根据经纬度找地名=" + getName("116.322987","39.983424")); 
	    }  
	      
	    /**
	     * 根据地址名称查找期坐标
	     * @param placeName
	     * @return
	     */
	    public static String getLocationByName(String placeName) {
	    	String url= "http://api.map.baidu.com/geocoder/v2/?city=北京市&address="
	    				+ placeName + "&output=json&ak="
	    				+ BAIDU_AK;
	    		return  loadJSON(url);  
	    }
	    
	    public static String getName(String lng, String lat){  
	         String url = "http://api.map.baidu.com/geocoder/v2/?city=北京是&location="
	        		 + lat + ","+ lng + "&output=json&pois=1&ak=" + BAIDU_AK;  
	            return  loadJSON(url);  
	    }  
	      
	    public static String getNameList(String lng, String lat){  
	         String url = "http://api.map.baidu.com/geocoder/v2/?location="
	        		 + lat + ","+ lng + "&output=json&pois=1&ak=" + BAIDU_AK;  
	            return  loadJSON(url);  
	    }  
	      
	    
	     public static String loadJSON (String url) {  
	            StringBuilder json = new StringBuilder();  
	            try {  
	                URL oracle = new URL(url);  
	                URLConnection yc = oracle.openConnection();  
	                BufferedReader in = new BufferedReader(new InputStreamReader(  
	                                            yc.getInputStream()));  
	                String inputLine = null;  
	                while ( (inputLine = in.readLine()) != null) {  
	                    json.append(inputLine);  
	                }  
	                in.close();  
	            } catch (MalformedURLException e) {  
	            } catch (IOException e) {  
	            }  
	            return json.toString();  
	        }   
}
